package com.xxx.video.mine.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.utils.DimensionUtil;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.mine.beans.FeedBackImageBean;

import butterknife.BindView;

public class FeedBackPhotoListAdapter extends BaseRecycleViewAdapter<FeedBackImageBean, FeedBackPhotoListAdapter.MyHolder> {

    public FeedBackPhotoListAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public MyHolder onCreateDefaultViewHolder(View view, int viewType) {
        return new MyHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        return R.layout.item_feed_back_photo_list;
    }

    @Override
    public int getItemCount() {
        if (ListUtils.isEmpty(getData())) {
            return 1;
        }
        if (getData().size() == 5) {
            return 5;
        } else {
            return getData().size() + 1;
        }
    }

    class MyHolder extends BaseViewHolder {

        @BindView(R.id.iv_item_feed_back_photo_list)
        ImageView ivItemFeedBackPhotoList;

        public MyHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            if (!ListUtils.isEmpty(getData()) && position != getData().size()) {
                FeedBackImageBean feedBackImageBean = getItem(position);
                Glide.with(context).load(feedBackImageBean.filePath)
                        .apply(new RequestOptions().transform(new CenterCrop(),new RoundedCorners(DimensionUtil.dpToPx(10))))
                        .into(ivItemFeedBackPhotoList);
            } else {
                Glide.with(context).load(R.drawable.ic_feed_back_add_photo).into(ivItemFeedBackPhotoList);
            }
            ivItemFeedBackPhotoList.setOnClickListener(v -> {
                if (ListUtils.isEmpty(getData()) || position == getData().size()) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(null, position, 1);
                    }
                }
            });
        }
    }
}
